package com.prototype.deepclone;

public class TestDeepCopy {
    public static void main(String[] args) {
        DeepCloneTarget target = new DeepCloneTarget("target", "122");
        DeepPrototype deepPrototype = new DeepPrototype("测试深拷贝", target);

        //开始拷贝对象
        DeepPrototype type1 = (DeepPrototype) deepPrototype.clone();
        DeepPrototype type2 = (DeepPrototype) deepPrototype.clone();
        DeepPrototype type3 = (DeepPrototype) deepPrototype.clone();
        DeepPrototype type4 = (DeepPrototype) deepPrototype.clone();

        //todo: 方法一：通过重写clone方法，对基本类型，引用类型属性单独处理实现深拷贝
        //检查DeepPrototype对象的DeepCloneTarget 属性是否同一个（是否实现了深拷贝）
        System.out.println(type1.getDeepCloneTarget() == type2.getDeepCloneTarget());  //false 说明实现了深拷贝
        System.out.println(type1.getDeepCloneTarget().hashCode());
        System.out.println(type2.getDeepCloneTarget().hashCode());

        System.out.println("************* 序列化方式实现深拷贝 ***********");

        //todo:方法二：通过序列化的方式实现深拷贝
        DeepPrototype deep1 = (DeepPrototype) deepPrototype.deepCopyBySerializable();
        DeepPrototype deep2 = (DeepPrototype) deepPrototype.deepCopyBySerializable();
        DeepPrototype deep3 = (DeepPrototype) deepPrototype.deepCopyBySerializable();
        System.out.println(deep1.getDeepCloneTarget() == deep2.getDeepCloneTarget());  //false 说明实现了深拷贝
        System.out.println(deep1.getDeepCloneTarget().hashCode());
        System.out.println(deep2.getDeepCloneTarget().hashCode());
    }
}
